package com.liuwei.msmall.cart.controller;

import com.liuwei.msmall.cart.service.CartService;
import com.liuwei.msmall.cart.vo.CartItemVo;
import com.liuwei.msmall.cart.vo.CartVo;
import com.liuwei.msmall.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @Classname CartController
 * @Description TODO
 * @Date 2020/6/28 1:37
 * @Created by 刘
 */
@Slf4j
@Controller
public class CartController {

    @Autowired
    private CartService cartService;


    //根据用户id查出当前的购物项
    @GetMapping("/getCurrentUserCartItem")
    @ResponseBody//远程调用接口返回json数据
    public List<CartItemVo> getCurrentUserCartItem(){
        try {
            //获取当前用户已经选中的购物项
            List<CartItemVo> cartItemVos=cartService.getCurrentUserCartItem();
            return cartItemVos;
        }catch (Exception e){
            log.error("e:{}",e);
            return null;
        }

    }


    //查出购物信息，并跳转页面
    @GetMapping("/cart.html")
    public String cartPage(Model model) throws ExecutionException, InterruptedException {

        //查出当前已经添加进购物车的商品
        CartVo cart= cartService.getCart();
        log.info("cart:{}",cart);
        model.addAttribute("cart",cart);
        return "cart";
    }


    //添加商品到购物车，然后重定向到添加成功地址，防止用户使用添加地址疯狂增加商品
    @GetMapping("/addCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("count") Integer count, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        //可以使用
        //redirectAttributes.addFlashAttribute();//将数据放在session中，而且只能拿一次
        //redirectAttributes.addAttribute()将数据放在url中，会自动拼接到url后，相当于放在请求域中的一个参数
        //将商品保存在redis中
        cartService.addCart(skuId, count);
        //拿到skuId和数量
        log.info("商品skuid:{}",skuId);
        log.info("购买的数量count:{}",count);
//        redirectAttributes.addAttribute("skuId",skuId);
//        return "redirect:http://cart.msmall.com/addCartSuccess.html?skuId="+skuId.toString();
        return "redirect:http://cart.msmall.com/cart.html";
    }

    //此地址在查出redis中保存的购物车中的商品信息
    @GetMapping("/addCartSuccess.html")
    public String addSuccessCart(@RequestParam(value = "skuId",required = false) String skuId,Model model){
        //在根据skuId查出对应的商品信息
        CartItemVo cartItemVo = cartService.getCartItemBySkuId(skuId);
        model.addAttribute("cartItem",cartItemVo);
        return "success";
    }


    //修改购物项的信息
    @GetMapping("/updateCartChecked")
    public String updateCartChecked(String skuId,String checked) throws ExecutionException, InterruptedException {

        cartService.updateCartChecked(skuId,checked);
        return "redirect:http://cart.msmall.com/cart.html";
    }

    //修改购物项的商品数量
    @GetMapping("/updateCartCount")
    public String updateCartCount(String skuId,String count,Model model) throws ExecutionException, InterruptedException {
        //根据skuId修改缓存中的商品数量
        cartService.updateCartCount(skuId,count);

        return "redirect:http://cart.msmall.com/cart.html";
    }

    //根据指定的skuid删除对应购物车的数据
    @GetMapping("/deleteItem")
    public String deleteItem(Long skuId){
//        System.out.println(skuId);
        cartService.deleteItemBySkuId(skuId);
        return "redirect:http://cart.msmall.com/cart.html";
    }


    //根据skuId批量删除，购物车的商品信息
    @GetMapping("deleteBatchItem")
    public String deleteBatchItem(String[] skuIds){

        cartService.deleteBatchItemBySkuId(skuIds);
        return "redirect:http://cart.msmall.com/cart.html";
    }

    @PostMapping("/clearCart")
    @ResponseBody
    public R clearCart(List<Long> skuIds){

        if (CollectionUtils.isEmpty(skuIds)){
            return R.ok();
        }
        cartService.deleteBatchItemBySkuId(skuIds.stream().map(id->id.toString())
                .collect(Collectors.toList()).toArray(new String[skuIds.size()]));
        return R.ok();
    }



    //批量全选和全不选
    @GetMapping("/updateBatchCartChecked")
    public String updateBatchCartChecked(@RequestParam("skuIds") String[] skuIds,@RequestParam("checked") String checked){
        cartService.updateBatchCartChecked(skuIds,checked);
        return "redirect:http://cart.msmall.com/cart.html";
    }

    //获取当前用户购物车数量
    @GetMapping("/getCartNum")
    @ResponseBody
    public R getCartNum(){
        Integer num = cartService.getCartNum();
        return R.ok().setData(num);
    }

}
